package com.xx.bysj.security;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xx.bysj.utils.NumberUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 前后端不分离方案，前后端分离需采用json
 */
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    // springMVC重定向
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        JSONObject returnObj = new JSONObject();
        try {
            returnObj.put("code", NumberUtils.TWO_HUNDRED);
            returnObj.put("message","登录成功" );
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(returnObj.toString());
        response.getWriter().flush();

    }
}
